﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Wordprocessing;

namespace component
{
    public partial class DocxBuilderComponent
    {
        public static List<OpenXmlElement> returnDeveloperConceptualElements(XmlNode doc, bool isContent)
        {
            List<OpenXmlElement> developerConceptualElements = new List<OpenXmlElement>();

            //Добавляем название топика из списка топиков
            string[] partTopicId = topicID.Split('-');
            Paragraph pNameTopic = new Paragraph();
            ParagraphProperties pPrNameTopic = new ParagraphProperties();
            ParagraphStyleId pStyleNameTopic = new ParagraphStyleId() { Val = "NameTopic" };
            pPrNameTopic.Append(pStyleNameTopic);
            pNameTopic.Append(pPrNameTopic);
            BookmarkStart markStart = new BookmarkStart() { Name = "topicId_" + partTopicId[0], Id = topicID };
            BookmarkEnd markEnd = new BookmarkEnd() { Id = topicID };
            Run rNameTopic = new Run();
            if (isFirst && !isContent)
            {
                rNameTopic.Append(new Text(topicTitleAndId[topicID]));
                pNameTopic.Append(rNameTopic);
                pNameTopic.Append(markStart);
                pNameTopic.Append(markEnd);
                developerConceptualElements.Add(pNameTopic);
                isFirst = false;
            }
            else
            {
                Break brNameTopic = new Break() { Type = BreakValues.Page };
                rNameTopic.Append(brNameTopic);
                rNameTopic.Append(new Text(topicTitleAndId[topicID]));
                pNameTopic.Append(rNameTopic);
                pNameTopic.Append(markStart);
                pNameTopic.Append(markEnd);
                developerConceptualElements.Add(pNameTopic);
            }

            //узнаем, какой следующий тэг
            foreach (XmlNode node in doc)
            {
                switch (node.Name)
                {
                    case "title":
                        {
                            //добавляем заглавие
                            if (node.HasChildNodes)
                            {
                                Paragraph p = new Paragraph();
                                ParagraphProperties pPr = new ParagraphProperties();
                                ParagraphStyleId pStyleId = new ParagraphStyleId() { Val = "Title" };
                                pPr.Append(pStyleId);
                                p.Append(pPr);
                                foreach (OpenXmlElement r in elementPara(node))
                                    p.Append(r);
                                developerConceptualElements.Add(p);
                            }
                        }
                        break;
                    case "summary":
                        {
                            //добавляем вступление
                            if (node.HasChildNodes)
                            {
                                foreach (OpenXmlElement element in blockElement(node))
                                    developerConceptualElements.Add(element);
                            }
                        }
                        break;
                    case "introduction":
                        {
                            //добавляем предисловие
                            if (node.HasChildNodes)
                            {
                                foreach (OpenXmlElement element in blockElement(node))
                                    developerConceptualElements.Add(element);
                            }
                        }
                        break;
                    case "section":
                        {
                            //добавляем секцию
                            if (node.HasChildNodes)
                            {
                                foreach (OpenXmlElement section in elementSection(node))
                                    developerConceptualElements.Add(section);
                            }
                        }
                        break;
                    case "relatedTopics":
                        break;
                }
            }

            return developerConceptualElements;
        }

        public static List<OpenXmlElement> elementSection(XmlNode node)
        {
            List<OpenXmlElement> listSection = new List<OpenXmlElement>();

            if (node.HasChildNodes)
            {
                foreach (XmlNode element in node)
                {
                    if (element.NodeType == XmlNodeType.Element)
                    {
                        switch (element.Name)
                        {
                            case "title":
                                {
                                    if (element.HasChildNodes)
                                    {
                                        Paragraph p = new Paragraph();
                                        ParagraphProperties pPr = new ParagraphProperties();
                                        ParagraphStyleId pStyleId1 = new ParagraphStyleId() { Val = "Title" };
                                        pPr.Append(pStyleId1);
                                        p.Append(pPr);
                                        List<OpenXmlElement> para = elementPara(element);
                                        foreach (OpenXmlElement el in para)
                                            p.Append(el);
                                        listSection.Add(p);
                                    }
                                }
                                break;
                            case "content":
                                {
                                    foreach (OpenXmlElement el in blockElement(element))
                                        listSection.Add(el);
                                }
                                break;
                            case "sections":
                                {
                                    if (element.HasChildNodes)
                                    {
                                        foreach (XmlNode subSection in element)
                                        {
                                            if (subSection.Name == "section")
                                            {
                                                foreach (OpenXmlElement el in elementSection(subSection))
                                                    listSection.Add(el);
                                            }
                                        }
                                    }
                                }
                                break;
                        }
                    }
                }
            }

            return listSection;
        }
    }
}
